<?php

namespace App;

use DB;
use Illuminate\Database\Eloquent\Model;
use Storage;

/**
 * Class Banner
 *
 * @package App
 * @mixin \Eloquent
 */
class Tool extends Model
{
    public $timestamps = false;
    protected $table = 'kl_function_dic';

    //是否显示
    const DEL_OFF = 1;
    const DEL_ON = 0; 

    static $DelDisplay = [
        self::DEL_OFF => '否',
        self::DEL_ON => '是'
    ];

    //是否在首页显示
    const SHOW_OFF = 0;
    const SHOW_ON = 1;
    static $ShowDisplay = [
        self::SHOW_OFF => '不显示',
        self::SHOW_ON => '显示',
    ];
 
     //内部跳转位置
    const TYPE_WEB = 1;
    const TYPE_APP = 2;
	const TYPE_SHANG = 3;
	const TYPE_GAME = 4;
	//const TYPE_AD = 5;
    static $TypeDisplay = [
        self::TYPE_WEB => 'web',
        self::TYPE_APP => '帖子',
        self::TYPE_SHANG => '商家',
        self::TYPE_GAME => '游戏乐园',
		//self::TYPE_AD => '广告',
    ];
    /**
     * 添加工具
     * @param $request
     * @return array
     */
    static function ToolPublish($request)
    {
        $tool = new Tool();
        //tool
        if ($request->has('pic')) {
            $path = Storage::url(Postings::OssUpdate($request->pic));
            $tool->pic = $path;
        } else {
            return ['status' => 2, 'message' => '图片呢?'];
        }
        $tool->name = $request->name;
        $tool->url = $request->url;
		$tool->is_homepage = $request->is_homepage;
		$tool->is_del = $request->is_del;
		if($request->type){
			$tool->type = $request->type;
		}
		$tool->create_time = date('Y-m-d H:i:s', time());
        if (!$tool->save()) {
            return ['status' => 2, 'message' => '添加失败'];
        }

        return ['status' => 1, 'message' => '添加成功'];
    }


    /**
     * 修改工具
     * @param $request
     * @param $tool
     * @return array
     */
    static function ToolModify($request, $tool)
    {

        //tool
        if ($request->has('pic')) {
            $path = Storage::url(Postings::OssUpdate($request->pic));
            $tool->pic = $path;
        }

        $tool->name = $request->name;
        $tool->url = $request->url;
		$tool->is_homepage = $request->is_homepage;
		$tool->is_del = $request->is_del;  
		if($request->type){
			$tool->type = $request->type;
		}		

        if (!$tool->save()) {
            return ['status' => 2, 'message' => '修改失败'];
        }

        return ['status' => 1, 'message' => '修改成功'];
    }
	
	//删除工具
    static function Del($id, $request)
    {
        DB::beginTransaction();

        if (!Tool::where('id', $id)->update([
            'is_del' => Tool::DEL_ON
        ])) {
            DB::rollBack();

            return ['status' => 2, 'message' => '删除失败'];
        }

        DB::commit();

        return ['status' => 1, 'message' => '删除成功'];
    }
	
	//刷新
	static function Shuaxin($id, $request){
		DB::beginTransaction();

        if (!Tool::where('id', $id)->update([
            'create_time' => date('Y-m-d H:i:s',time())
        ])) {
            DB::rollBack();

            return ['status' => 2, 'message' => '刷新失败'];
        }

        DB::commit();

        return ['status' => 1, 'message' => '刷新成功'];
	}	

}
